/*
 * @Author: your name
 * @Date: 2021-04-02 18:17:02
 * @LastEditTime: 2021-04-06 18:51:21
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \inventory-apie:\hjimi\人脸辨识云\html\visitorOrder\commom\js\request.js
 */
import Vue from 'vue'; //引入vue
 let vm = new Vue();
 
function req(obj) {
    return new Promise((resolve, reject) => {
      const HOST = vm.BASEURL;
      var method = obj.method || "GET"; 
      var url = HOST + obj.url || ""; 
      var data = obj.data || {}; 
      var header = obj.header || {
          'Content-Type': obj.contentType || 'application/x-www-form-urlencoded;'
      }; 
      var success = obj.success; // 成功回调函数
      var fail = obj.fail; //表示失败后，要执行的回调函数
          let token = null;
            uni.request({
              url: url,
              data: data,
              method: method,
              header: Object.assign(header, { 'Authorization': uni.getStorageSync('Authorization')}),
              success: ((res) => {
				  if(res.data.code == 1) {
					  uni.removeStorageSync('wxUser')
					  uni.reLaunch({url:'/pages/login/login'})
				  }
                if (res.statusCode === 403 || res.statusCode === 401) {
                 // 错误处理，返回登录页
				  uni.removeStorageSync('wxUser')
                  uni.reLaunch({url:'/pages/login/login'})
                } else if(res.statusCode === 200) {
                   resolve(res)
                } else if(res.statusCode === 500) {
					uni.showToast({
					  title: '服务异常 请稍后重试',
					  icon: 'none',
					})
				}	
				 else {
                  uni.showToast({
                    title: res.data.errMsg,
                    icon: 'none',
                  })
                }
              }),
              fail: ((err) => {
                reject(err)
              })
          })
    })
  }
  export default req